/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.satyam.swingvalidator.commons;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JComponent;
import javax.swing.JTextField;
import com.satyam.swingvalidator.core.AbstractValidator;
import com.satyam.swingvalidator.core.Severity;

/**
 *
 * @author Satyam
 */
public class RegularExpressionValidator extends AbstractValidator{

    private final Pattern pattern;

    public RegularExpressionValidator(String regex) {
        this.pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    }

    @Override
    protected Severity getValidationCriteria(JComponent c) {

        String strToValidate = ((JTextField) c).getText();
        Matcher m = pattern.matcher(strToValidate);

        if (m.matches()) {
            return new Severity(Severity.NO_ERROR, "");
        } else {
            return new Severity(Severity.ERROR, "The field is not in proper format!");
        }

    }
    
    
    
}
